//
//  downloadCount.m
//  XMPPDemo
//
//  Created by Edison on 16/1/18.
//  Copyright © 2016年 Gnet. All rights reserved.
//


#import "downloadCounter.h"
#import "APPIdentificationManage.h"

@interface downloadCounter () <NSURLSessionDelegate>
@property (nonatomic, copy) NSURLSession *theSession;
@property (nonatomic, copy) NSString *jsonString;

@end

@implementation downloadCounter

- (instancetype)init {
	if (self = [super init]) {
		NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
		NSDateFormatter *formatter = [NSDateFormatter new];
		[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
		self.DEVICEID = [[APPIdentificationManage sharedAPPIdentificationManage]readUDID];
		self.HTTP = kDownloadCountingURL;
		self.APPID = [infoDict objectForKey:@"CFBundleIdentifier"];
		self.APPNAME = [infoDict objectForKey:@"CFBundleName"];
		self.APPVERSION = [infoDict objectForKey:@"CFBundleShortVersionString"];
		self.UPDATETIME = [formatter stringFromDate:[NSDate new]];
		self.APPNAME = [infoDict objectForKey:@"CFBundleName"];
	}
	return self;
}

+ (instancetype)sharedCounter {
	static id instance;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		instance = [[self alloc] init];
	});
	return instance;
}

-(void)couting {
	NSString *param = [self.jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	NSURL *tUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",self.HTTP,param]];
	NSMutableURLRequest* request =[NSMutableURLRequest requestWithURL:tUrl cachePolicy:0 timeoutInterval:10];
	[[self.theSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
		if (nil == error) {
			//返回数据的RESCODE为"0000"代表成功
			NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:1 error:nil];
			if ([@"0000" isEqualToString:[[dict objectForKey:@"HEAD"] objectForKey:@"RESCODE"]]) {
				NSLog(@"统计成功%@",dict );
			} else {
				NSLog(@"统计失败,返回数据为:%@",dict);
			}
		} else {
			NSLog(@"ERROR:%@",error);
		}
	}]resume];

}

#pragma mark- jsonString
-(NSString *)jsonString {
	if (nil == _jsonString) {
		_jsonString = [NSString stringWithFormat:@"JSON_PARAM={"
					 "\"HEAD\":{"
					 "\"VERSION\": \"NULL\","
					 "\"DEVICETYPE\": \"NULL\","
					 "\"USERID\": \"NULL\","
					 "\"DRIVERID\": \"NULL\","
					 "\"CLOUDID\": \"NULL\","
					 "\"SIGN\": \"NULL\","
					 "\"ZIP\": \"NULL\","
					 "},"
					 "\"INFO\": {"
					 "\"DEVICEID\": \"%@\","		//UUID
					 "\"APPID\": \"%@\","			//CFBundleIdentifier
					 "\"APPNAME\": \"%@\","			//CFBundleName
					 "\"APPDESC\": \"NULL\","
					 "\"APPPACKAGENAME\": \"NULL\","
					 "\"APPVERSION\": \"%@\","		//CFBundleShortVersionString
					 "\"APPTYPE\": \"NULL\","
					 "\"APPCONTENT\": \"NULL\","
					 "\"logoUrl\": \"NULL\","
					 "\"UPDATETIME\": \"%@\","		//时间格式必须正确yyyy-MM-dd HH:mm:ss
					 "\"APPCUTIMAGE\": \"NULL\","
					 "\"ANDROURL\": \"NULL\","
					 "\"IOSURL\": \"NULL\","
					 "},"
					 "}",
					 self.DEVICEID,
					 self.APPID,
					 self.APPNAME,
					 self.APPVERSION,
					 self.UPDATETIME];
	}
	return _jsonString;
}

-(NSURLSession *)theSession {
	if (nil == _theSession) {
			_theSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
		}
		return _theSession;
	}

@end
